//
//  
//
//  Created by admin on 2017年9月5日 星期二.
//  Copyright © 2017年 __MyCompanyName__. All rights reserved.
//

#import "LCTabBarController.h"
#import "UITabBarController+LCAddition.h"
#import "LCNavigationController.h"
#import "LCTabBar.h"
@interface LCTabBarController ()<UITabBarControllerDelegate,UITabBarDelegate>

@property (strong, nonatomic) NSDate *lastDate;

@end

@implementation LCTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    //tab setup
    [self addChildViewControllers];//创建
    self.delegate = self;

    LCTabBar *lcTabBar = [[LCTabBar alloc] init];
    @weakify(self)
    lcTabBar.btnClcikBlock = ^(){
        @strongify(self)
//        NSLog(@"中间的按钮被点击了");
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"LCPlayStoryboard" bundle:nil];
        LCViewController *controller = [sb instantiateInitialViewController];
        controller.vcInitDictData = @{@"PlaybackControl":@"YesHidden"};
        [self presentViewController:controller animated:YES completion:nil];
    };
    [self setValue:lcTabBar forKey:@"TabBar"];

}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    
    NSInteger index = [self.tabBar.items indexOfObject:item];
//    NSDate *date = [[NSDate alloc] init];
    if (index == 0) {
//        if (date.timeIntervalSince1970 - _lastDate.timeIntervalSince1970 < 0.5) {
//            [self animationWithIndex:index];//放大缩小动画.. 刷新旋转动画
//        }
    }
    
}
// 动画
- (void)animationWithIndex:(NSInteger) index {
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.18;
    pulse.repeatCount= 1;
    pulse.autoreverses= YES;
    pulse.fromValue= [NSNumber numberWithFloat:0.7];
    pulse.toValue= [NSNumber numberWithFloat:1.3];
    [[tabbarbuttonArray[index] layer]
     addAnimation:pulse forKey:nil];
    
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    UINavigationController *vc = tabBarController.selectedViewController;

    NSDate *date = [[NSDate alloc] init];
    if ([vc isEqual:viewController]) {
        if (tabBarController.selectedIndex == 0) {
            // 处理双击事件
            if (date.timeIntervalSince1970 - _lastDate.timeIntervalSince1970 < 0.5) {
                [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_TABBAR_DOUBLE_CLICK object:self userInfo:nil];
//                NSLog(@"NOTIFY_TABBAR_DOUBLE_CLICK");
            }
            _lastDate = date;
            return NO;
        }
    }
    return YES;
}
-(void)addChildViewControllers{
    //首页
    UIViewController *homeNavController = [UITabBarController lc_addChildControllerWithSbName:@"HomePage" title:@"首页" imageName:@"tab_shouyeicon_nor"];
    [self addChildViewController:homeNavController];
    
    //"历史记录"
    UIViewController *videoNavController = [UITabBarController lc_addChildControllerWithSbName:@"History" title:@"动态资讯"  imageName:@"tab_lsjlicon_nor"];
    [self addChildViewController:videoNavController];

    //奉献支持
    UIViewController *activityNavController = [UITabBarController lc_addChildControllerWithSbName:@"Fxzc" title:@"奉献支持" imageName:@"tab_fxzcicon_nor"];
    [self addChildViewController:activityNavController];
    
    //我的
    UIViewController *meNavController = [UITabBarController lc_addChildControllerWithSbName:@"Mine" title:@"我的" imageName:@"tab_myicon_nor"];
    [self addChildViewController:meNavController];
    
}


@end
